import React from 'react';
import {Spin, Card, Button, Row, Col, Table, Form} from 'antd';
import PropTypes from 'prop-types';
import routes from './routes';
import {FORM_OPTIONS, TABLE, FORM_ROW_OPTIONS, DATETIME_FORMAT} from '../constants';
import {updateApplyReport, getDetail} from './api';
import {formatDateTime, conventEnumValueToString} from '../utils';
import {salePlanAssembleMethod} from '../Enum';
import TextInput from 'Shared/components/TextInput';
import {injectIntl} from './intl';
import styles from './style.css';
import WrappedInputNumber from '../common/WrappedInputNumber';
import Return from '../common/Return';
const FormItem = Form.Item;

class UpdatePanel extends React.PureComponent {
    state = {
        loading: false,
        hasOperat: false,
        data: {},
        details: [],
    }
    componentDidMount() {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                        details: res.data.details,
                    });
                else
                    this.setState({loading: false});
            });
    }
    componentWillUnmount() {
        this.isunmount = true;
    }
    onChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    onClickOkOnPopconfirm = () => {
        this.props.onCancel();
    }
    onSubmit = () => {
        const data = this.state.data;
        const details = this.state.details;
        const condition = {
            remark: data.remark,
            rowVersion: data.rowVersion,
            details: details.map(v => ({
                id: v.id,
                businessType: v.businessType,
                materialGroupKDBOMCode: v.materialGroupKDBOMCode,
                isNewModel: v.isNewModel === 1,
                n1SalesTargetQuantity: v.n1SalesTargetQuantity,
                n2SalesTargetQuantity: v.n2SalesTargetQuantity,
                n3SalesTargetQuantity: v.n3SalesTargetQuantity,
                n4SalesTargetQuantity: v.n4SalesTargetQuantity,
                n5SalesTargetQuantity: v.n5SalesTargetQuantity,
                n6SalesTargetQuantity: v.n6SalesTargetQuantity,
                n7SalesTargetQuantity: v.n7SalesTargetQuantity,
                n8SalesTargetQuantity: v.n8SalesTargetQuantity,
                n9SalesTargetQuantity: v.n9SalesTargetQuantity,
                n10SalesTargetQuantity: v.n10SalesTargetQuantity,
                n11SalesTargetQuantity: v.n11SalesTargetQuantity,
                n12SalesTargetQuantity: v.n12SalesTargetQuantity,
                remark: v.remark,
            })),
        };
        this.setState({loading: true});
        updateApplyReport(this.props.id, condition).then(res => {
            if(res.ok)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    onChangeAll = (value, id, e) => {
        const details = this.state.details || [];
        const name = e.target.name;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    [name]: value
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    onChangeQuantity = (value, name, e) => {
        const details = this.state.details || [];
        const id = e.target.id;
        const tmp = details.map(d => {
            if(d.id === id)
                return Object.assign({}, d, {
                    [name]: value
                });
            return d;
        });
        this.setState({
            details: tmp,
            hasOperat: true
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data, details, loading} = this.state;
        const columns = [
            {
                title: formatMessage({
                    id: 'updatePanel.column.businessType',
                    defaultMessage: '装配方式'
                }),
                dataIndex: 'businessType',
                render: text => conventEnumValueToString(salePlanAssembleMethod, text)
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.versionCode',
                    defaultMessage: '版型编号'
                }),
                dataIndex: 'versionCode'
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.versionName',
                    defaultMessage: '版型名称'
                }),
                dataIndex: 'versionName'
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.modelCode',
                    defaultMessage: '车型代码'
                }),
                dataIndex: 'modelCode'
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.modelName',
                    defaultMessage: '车型名称'
                }),
                dataIndex: 'modelName'
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.materialGroupKDBOMCode',
                    defaultMessage: 'KDBOM号或参考物料组'
                }),
                dataIndex: 'materialGroupKDBOMCode'
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.isNewModel',
                    defaultMessage: '是否新车型'
                }),
                dataIndex: 'isNewModel',
                render: text => text ? formatMessage({
                    id: 'updatePanel.column.isExpectedListYes',
                    defaultMessage: '是'
                }) : formatMessage({
                    id: 'updatePanel.column.isExpectedListNo',
                    defaultMessage: '否'
                })
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n1SalesTargetQuantity',
                    defaultMessage: 'N+1计划数'
                }),
                dataIndex: 'n1SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n1SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n2SalesTargetQuantity',
                    defaultMessage: 'N+2计划数'
                }),
                dataIndex: 'n2SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n2SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n3SalesTargetQuantity',
                    defaultMessage: 'N+3计划数'
                }),
                dataIndex: 'n3SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n3SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n4SalesTargetQuantity',
                    defaultMessage: 'N+4计划数'
                }),
                dataIndex: 'n4SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n4SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n5SalesTargetQuantity',
                    defaultMessage: 'N+5计划数'
                }),
                dataIndex: 'n5SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n5SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n6SalesTargetQuantity',
                    defaultMessage: 'N+6计划数'
                }),
                dataIndex: 'n6SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n6SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n7SalesTargetQuantity',
                    defaultMessage: 'N+7计划数'
                }),
                dataIndex: 'n7SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n7SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n8SalesTargetQuantity',
                    defaultMessage: 'N+8计划数'
                }),
                dataIndex: 'n8SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n8SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n9SalesTargetQuantity',
                    defaultMessage: 'N+9计划数'
                }),
                dataIndex: 'n9SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n9SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n10SalesTargetQuantity',
                    defaultMessage: 'N+10计划数'
                }),
                dataIndex: 'n10SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n10SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n11SalesTargetQuantity',
                    defaultMessage: 'N+11计划数'
                }),
                dataIndex: 'n11SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n11SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.n12SalesTargetQuantity',
                    defaultMessage: 'N+12计划数'
                }),
                dataIndex: 'n12SalesTargetQuantity',
                render: (text, record) => (
                    <FormItem>
                        <WrappedInputNumber
                            id={record.id}
                            name="n12SalesTargetQuantity"
                            value={text}
                            onBlur={this.onChangeQuantity} />
                    </FormItem>
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.total',
                    defaultMessage: '总计'
                }),
                dataIndex: 'total',
                render: (text, record) => (
                    (record.n1SalesTargetQuantity + record.n2SalesTargetQuantity + record.n3SalesTargetQuantity + record.n4SalesTargetQuantity + record.n5SalesTargetQuantity + record.n6SalesTargetQuantity + record.n7SalesTargetQuantity + record.n8SalesTargetQuantity + record.n9SalesTargetQuantity + record.n10SalesTargetQuantity + record.n11SalesTargetQuantity + record.n12SalesTargetQuantity)
                )
            },
            {
                title: formatMessage({
                    id: 'updatePanel.column.remark',
                    defaultMessage: '备注'
                }),
                dataIndex: 'remark',
                render: (text, record) => (
                    <FormItem>
                        <TextInput
                            id={record.id}
                            name="remark"
                            value={text}
                            onBlur={this.onChangeAll} />
                    </FormItem>
                )
            },
        ];
        return (
            <div className="form-standard">
                <Spin spinning={this.state.loading}>
                    <Card title={formatMessage({
                        id: 'updatePanel.column.common',
                        defaultMessage: 'KD生产计划管理'
                    })}>
                        <Form>
                            <Row>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'updatePanel.operation.planYear',
                                        defaultMessage: '计划年份'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.planYear}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={this.props.intl.formatMessage({
                                        id: 'updatePanel.form.brandCode',
                                        defaultMessage: '品牌'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.brandCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'updatePanel.operation.marketDepartmentName',
                                        defaultMessage: '大区'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.marketDepartmentName}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'updatePanel.operation.country',
                                        defaultMessage: '国家'
                                    })} {...FORM_OPTIONS.item}>
                                        {data.country}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={this.props.intl.formatMessage({
                                        id: 'updatePanel.form.dealerCode',
                                        defaultMessage: '总代编号'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.dealerCode}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={this.props.intl.formatMessage({
                                        id: 'updatePanel.form.creatorName',
                                        defaultMessage: '创建人'
                                    })}{...FORM_OPTIONS.item}>
                                        {data.creatorName}
                                    </FormItem>
                                </Col>
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem label={formatMessage({
                                        id: 'updatePanel.operation.createTime',
                                        defaultMessage: '创建时间'
                                    })}{...FORM_OPTIONS.item}>
                                        {formatDateTime(data.createTime, DATETIME_FORMAT)}
                                    </FormItem>
                                </Col>
                            </Row>
                            <Row className="row-margin">
                                <Col>
                                    <FormItem label={formatMessage({
                                        id: 'updatePanel.operation.remark',
                                        defaultMessage: '备注'
                                    })} {...FORM_ROW_OPTIONS.item}>
                                        <TextInput
                                            name="remark"
                                            type="textarea"
                                            value={data.remark}
                                            onBlur={this.onChange} />
                                    </FormItem>
                                </Col>
                            </Row>
                        </Form>
                    </Card>
                    <Card
                        title={formatMessage({
                            id: 'updatePanel.operation.detail',
                            defaultMessage: 'KD生产计划清单'
                        })} extra={<div>
                        </div>}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            pagination={false}
                            dataSource={details}
                            {...TABLE} />
                    </Card>
                    <Card className={styles.optionBtn}>
                        <Button
                            type="primary"
                            onClick={this.onSubmit}
                            loading={loading}>
                            {formatMessage({
                                id: 'updatePanel.operation.onSubmitPass',
                                defaultMessage: '保存'
                            })}
                        </Button>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Return
                        disabled={this.state.loading}
                        onConfirm={this.onClickOkOnPopconfirm}
                        showWarning={this.state.hasOperat} />
                </div>
            </div>
        );
    }
}

UpdatePanel.propTypes = {
    successReturn: PropTypes.func.isRequired,
    onCancel: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    history: PropTypes.object,
    id: PropTypes.string,
    intl: PropTypes.object,
};

import {connect} from 'react-redux';
import {
    tableSearch,
    saveQueryDealer
} from './actions';

const mapStateToProps = state => ({
});
const mapDispatchToProps = (dispatch, props) => ({
    onSelect: data => dispatch(saveQueryDealer(data)),
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(UpdatePanel));
